package hsp.admin.app.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import hsp.admin.api.ApiResult;
import hsp.admin.app.base.po.RoleAppRelationPO;
import hsp.admin.app.base.service.IRoleAppRelationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "RoleAppRelationController", description = "角色程序关系表")
@RestController
@RequestMapping("base/role/app/relation")
public class RoleAppRelationController {

    private final IRoleAppRelationService service;

    @Autowired
    public RoleAppRelationController(
        IRoleAppRelationService service
    ){
        this.service = service;
    }

    @Operation(summary = "根据角色主键获取角色程序关系")
    @GetMapping("list")
    public ApiResult<List<RoleAppRelationPO>> getRoleAppRelationList(RoleAppRelationPO po){
        LambdaQueryWrapper<RoleAppRelationPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoleAppRelationPO::getRoleId, po.getRoleId());
        List<RoleAppRelationPO> list = service.list(wrapper);
        return ApiResult.success(list);
    }

    @Operation(summary = "编辑角色程序关系")
    @PostMapping("allocateAppByRoleId")
    public ApiResult<?> postRoleAppRelation(@RequestParam("roleId") long roleId, @RequestParam(value = "appIds", required = false) int[] appIds){
        service.saveBatchByRoleIdAndAppIds(roleId, appIds);
        return ApiResult.success();
    }

}
